let app = getApp()

// pages/input/input.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var user=wx.getStorageSync('user') || {};  
    var userInfo=wx.getStorageSync('userInfo') || {}; 
    // if((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600))&&(!userInfo.nickName)){
    if(true) {
       wx.login({
       success: function(res){ 
           if(res.code) {
               wx.getUserInfo({
                   success: function (res) {
                       var objz={};
                       objz.avatarUrl=res.userInfo.avatarUrl;
                       objz.nickName=res.userInfo.nickName;
                       //console.log(objz);
                       wx.setStorageSync('userInfo', objz);//存储userInfo
                   }
               });
               var d=app.globalData;//这里存储了appid、secret、token串 
               //得到openId
               var l='https://api.weixin.qq.com/sns/jscode2session?appid='+d.appid+'&secret='+d.secret+'&js_code='+res.code+'&grant_type=authorization_code';  
               wx.request({  
                   url: l,  
                   data: {},  
                   method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
                   // header: {}, // 设置请求的 header  
                   success: function(res){ 
                       var obj={};
                       obj.openid=res.data.openid;  
                       obj.expires_in=Date.now()+res.data.expires_in;  
                       //console.log(obj);
                       wx.setStorageSync('user', obj);//存储openid  
                   }  
               });
           }else {
               console.log('获取用户登录态失败！' + res.errMsg)
           }          
       }  
     }); 
   }
  }

})